﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using RCA1._0;
using System.Data.OleDb;

namespace Royal_1._0_DAL_WS
{
    public class LoaiTruSoDAO: DataProvider
    {
        public DataTable LayBang()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAITRUSO";
            OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        #region "Lấy danh sách"

        public List<LoaiTruSoDTO> LayDanhSach()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAITRUSO";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            LoaiTruSoDTO tempDTO;
            List<LoaiTruSoDTO> dsLOAITRUSO = new List<LoaiTruSoDTO>();
            while (dr.Read())
            {
                tempDTO = new LoaiTruSoDTO();
                tempDTO.IDLoaiTruSo = Convert.ToInt32(dr["IDLoaiTruSo"]);
                tempDTO.TenLoaiTruSo = dr["TenLoaiTruSo"].ToString();
                dsLOAITRUSO.Add(tempDTO);
            }
            return dsLOAITRUSO;
        }

        #endregion


        #region "Lấy danh sách mã loại"

        public List<LoaiTruSoDTO> LayMaLoaiTruSo()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAICONGTY";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            LoaiTruSoDTO tempDTO;
            List<LoaiTruSoDTO> dsLOAITRUSO = new List<LoaiTruSoDTO>();
            while (dr.Read())
            {
                tempDTO = new LoaiTruSoDTO();
                tempDTO.IDLoaiTruSo = Convert.ToInt32(dr["IDLoaiTruSo"]);
                dsLOAITRUSO.Add(tempDTO);
            }
            return dsLOAITRUSO;
        }

        #endregion


        #region "Tìm kiếm"
        public LoaiTruSoDTO TimTheoMa(int MaLoaiTruSo)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAITRUSO Where IDLoaiTruSo=" + MaLoaiTruSo.ToString();
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            dr.Read();
            LoaiTruSoDTO tempDTO = new LoaiTruSoDTO();
            tempDTO.IDLoaiTruSo = Convert.ToInt32(dr["IDLoaiTruSo"]);
            tempDTO.TenLoaiTruSo = dr["TenLoaiTruSo"].ToString();
            cn.Close(); return tempDTO;
        }

        #endregion


        #region "Thêm"

        public void Them(LoaiTruSoDTO tempDTO)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "insert into LOAITRUSO(TenLoaiTruSo) values (@TenLoaiTruSo)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenLoaiTruSo", OleDbType.LongVarWChar).Value = tempDTO.TenLoaiTruSo;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        #endregion


        #region "Xóa"

        public void Xoa(int MaLoaiTruSo)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "delete from LOAITRUSO Where IDLoaiTruSo = " + MaLoaiTruSo.ToString();
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        #endregion


        #region "Sửa"

        public void Sua(LoaiTruSoDTO tempDTO)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "update LOAITRUSO set TenLoaiTruSo = @TenLoaiTruSo where IDLoaiTruSo = @IDLoaiTruSo";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenLoaiTruSo", OleDbType.LongVarWChar).Value = tempDTO.TenLoaiTruSo;
            cmd.Parameters.Add("@IDLoaiTruSo", OleDbType.Integer).Value = tempDTO.IDLoaiTruSo;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        #endregion

    }
}